package com.qf.kady.ssm.service.impl;

import com.qf.kady.ssm.entity.ResultVO;
import com.qf.kady.ssm.mapper.BookMapper;
import com.qf.kady.ssm.mapper.DropLogMapper;
import com.qf.kady.ssm.mapper.UserMapper;
import com.qf.kady.ssm.pojo.Book;
import com.qf.kady.ssm.pojo.DropLog;
import com.qf.kady.ssm.pojo.User;
import com.qf.kady.ssm.service.BookService;
import com.qf.kady.ssm.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/4/21
 * \* Time: 11:50
 * \* 千峰JAVA2101班
 * \* Description: 书品业务层
 * \
 */

@Service
@Transactional
@Slf4j
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private DropLogMapper dropLogMapper;

    @Override
    public List<Book> getBookListByCondition(String keyword,Long readerId,Long readerIdForLendFilter) {
        List<Book> list = bookMapper.getListByCondition(keyword != null ? "%"+keyword.trim()+"%" : "%%",readerId,readerIdForLendFilter);
        return list;
    }

    @Override
    public Book getBookDetailById(Long bookId) {
        return bookMapper.selectBookWithClassInfoById(bookId);
    }

    @Override
    public boolean addBook(Book book) {
        boolean res = bookMapper.addBook(book);
        return res;
    }

    @Override
    public boolean updateBook(Book book) {
        boolean res = bookMapper.updateBookById(book);
        return res;
    }

    @Override
    public ResultVO deleteBook(Long bookId) {
        DropLog res = dropLogMapper.findByCondition(bookId, null);
        if(res!=null){
            return ResultVO.error("删除失败，书品正在被借阅，无法删除！",null);
        }
        bookMapper.deleteBookById(bookId);
        return ResultVO.ok("删除成功",null);
    }

    @Override
    public ResultVO updateBookAndDropLog(DropLog dropLog,Long plusCount) {
        Book findBook = bookMapper.selectBookWithClassInfoById(dropLog.getBookId());
        //根据bookId查询到书品
        if(findBook==null){
            return ResultVO.error("未找到对应的书品！",null);
        }
        //借阅时判断库存
        if(plusCount<0&&findBook.getNumber()==0){
            return ResultVO.error("该书品已被借完",null);
        }
        findBook.setNumber(findBook.getNumber()+plusCount);
        boolean result = bookMapper.updateBookById(findBook);
        if(!result){
            return ResultVO.error("库存更新错误",null);
        }

        if(plusCount>0){
            DropLog resDropLog = dropLogMapper.findByCondition(dropLog.getBookId(), dropLog.getReaderId());
            if(resDropLog==null){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResultVO.error("未找到对应借阅记录！",null);
            }
            resDropLog.setBackDate(new Date());
            //更新归还日期
            boolean updateRes = dropLogMapper.updateDropLog(resDropLog);

            return ResultVO.error("归还成功",null);
        }else{
            boolean addRes = dropLogMapper.addDropLog(dropLog);

            return ResultVO.error("借阅成功",null);
        }
    }

}
